package com.ruoyi.video.controller;

import java.util.Comparator;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.video.domain.VideoGroup;
import com.ruoyi.video.service.IVideoGroupService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 分组Controller
 * 
 * @author ruoyi
 * @date 2024-12-09
 */
@RestController
@RequestMapping("/video/group")
public class VideoGroupController extends BaseController
{
    @Autowired
    private IVideoGroupService videoGroupService;

    /**
     * 查询分组列表
     */
    @GetMapping("/select")
    public AjaxResult select(VideoGroup videoGroup)
    {
        List<VideoGroup> list = videoGroupService.selectVideoGroupList(videoGroup);
        list.sort(Comparator.comparingInt(VideoGroup::getSorts));
        return success(list);
    }


    /**
     * 查询分组列表
     */
    @PreAuthorize("@ss.hasPermi('video:group:list')")
    @GetMapping("/list")
    public TableDataInfo list(VideoGroup videoGroup)
    {
        startPage();
        List<VideoGroup> list = videoGroupService.selectVideoGroupList(videoGroup);
        return getDataTable(list);
    }

    /**
     * 导出分组列表
     */
    @PreAuthorize("@ss.hasPermi('video:group:export')")
    @Log(title = "分组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VideoGroup videoGroup)
    {
        List<VideoGroup> list = videoGroupService.selectVideoGroupList(videoGroup);
        ExcelUtil<VideoGroup> util = new ExcelUtil<VideoGroup>(VideoGroup.class);
        util.exportExcel(response, list, "分组数据");
    }

    /**
     * 获取分组详细信息
     */
    @PreAuthorize("@ss.hasPermi('video:group:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(videoGroupService.selectVideoGroupById(id));
    }

    /**
     * 新增分组
     */
    @PreAuthorize("@ss.hasPermi('video:group:add')")
    @Log(title = "分组", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VideoGroup videoGroup)
    {
        videoGroup.setCreateBy(getUsername());
        return toAjax(videoGroupService.insertVideoGroup(videoGroup));
    }

    /**
     * 修改分组
     */
    @PreAuthorize("@ss.hasPermi('video:group:edit')")
    @Log(title = "分组", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VideoGroup videoGroup)
    {
        videoGroup.setUpdateBy(getUsername());
        return toAjax(videoGroupService.updateVideoGroup(videoGroup));
    }

    /**
     * 删除分组
     */
    @PreAuthorize("@ss.hasPermi('video:group:remove')")
    @Log(title = "分组", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(videoGroupService.deleteVideoGroupByIds(ids));
    }
}
